package visao;

import conexao.ConectEvent;
import controle.MasterControl;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import javax.swing.event.MenuListener;
import util.Constantes;

public final class SadGUI_Cliente extends javax.swing.JFrame implements Teste {
// Instancia da classe SuporteEventos responsavel observar as ações e disparar os eventos

  SuporteEventos suporteEventos = new SuporteEventos(this);
  MasterControl c;

  public SadGUI_Cliente(MasterControl c) {
    initComponents();
    addOuvintes();
    this.c = c;
  }

  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btProfessor = new javax.swing.JButton();
        btDisciplina = new javax.swing.JButton();
        btArqDisc = new javax.swing.JButton();
        btArqProf = new javax.swing.JButton();
        panelMSG = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtMSG = new javax.swing.JTextArea();
        btEnviar = new javax.swing.JButton();
        panelUser = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listUser = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtArea = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        conexao = new javax.swing.JMenu();
        novaConexao = new javax.swing.JMenuItem();
        fecharConexao = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        sair = new javax.swing.JMenuItem();
        editar = new javax.swing.JMenu();
        preferencias = new javax.swing.JMenuItem();
        sobre = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Sistema de Geração de Provas");
        setMinimumSize(getPreferredSize());

        jToolBar1.setRollover(true);
        jToolBar1.setAutoscrolls(true);
        jToolBar1.setPreferredSize(new java.awt.Dimension(41, 41));

        btProfessor.setActionCommand("Novo Professor");
        btProfessor.setFocusable(false);
        btProfessor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btProfessor.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btProfessor);

        btDisciplina.setActionCommand("Nova Disciplina");
        btDisciplina.setEnabled(false);
        btDisciplina.setFocusable(false);
        btDisciplina.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btDisciplina.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btDisciplina);

        btArqDisc.setActionCommand("Relatorio Disciplina");
        btArqDisc.setFocusable(false);
        btArqDisc.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btArqDisc.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btArqDisc);

        btArqProf.setActionCommand("Relatorio Professor");
        btArqProf.setFocusable(false);
        btArqProf.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btArqProf.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btArqProf);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        panelMSG.setBorder(javax.swing.BorderFactory.createTitledBorder("Mensagens"));
        panelMSG.setMinimumSize(getPreferredSize());
        panelMSG.setLayout(new javax.swing.BoxLayout(panelMSG, javax.swing.BoxLayout.LINE_AXIS));

        txtMSG.setColumns(20);
        txtMSG.setRows(5);
        jScrollPane1.setViewportView(txtMSG);

        panelMSG.add(jScrollPane1);

        btEnviar.setText("Enviar");
        btEnviar.setMaximumSize(new java.awt.Dimension(63, 33));
        btEnviar.setMinimumSize(new java.awt.Dimension(63, 33));
        btEnviar.setName(""); // NOI18N
        btEnviar.setPreferredSize(new java.awt.Dimension(73, 33));
        panelMSG.add(btEnviar);

        getContentPane().add(panelMSG, java.awt.BorderLayout.PAGE_END);

        panelUser.setBorder(javax.swing.BorderFactory.createTitledBorder("Usuários"));
        panelUser.setLayout(new java.awt.BorderLayout());

        listUser.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "<Ninguem Conectado>" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listUser.setPreferredSize(new java.awt.Dimension(150, 80));
        jScrollPane2.setViewportView(listUser);

        panelUser.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        getContentPane().add(panelUser, java.awt.BorderLayout.LINE_END);

        jScrollPane3.setBorder(javax.swing.BorderFactory.createTitledBorder("Mensagens"));
        jScrollPane3.setPreferredSize(new java.awt.Dimension(470, 370));

        txtArea.setColumns(20);
        txtArea.setEditable(false);
        txtArea.setRows(5);
        txtArea.setPreferredSize(new java.awt.Dimension(440, 340));
        jScrollPane3.setViewportView(txtArea);

        getContentPane().add(jScrollPane3, java.awt.BorderLayout.CENTER);

        conexao.setText("Conexão");

        novaConexao.setText("Iniciar ...");
        novaConexao.setActionCommand("Nova Conexao");
        conexao.add(novaConexao);

        fecharConexao.setText("Fechar...");
        fecharConexao.setActionCommand("Fechar");
        conexao.add(fecharConexao);
        conexao.add(jSeparator1);

        sair.setText("Sair");
        conexao.add(sair);

        jMenuBar1.add(conexao);

        editar.setText("Editar");

        preferencias.setText("Preferências");
        preferencias.setActionCommand("Preferencias");
        editar.add(preferencias);

        jMenuBar1.add(editar);

        sobre.setText("Sobre");
        jMenuBar1.add(sobre);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents
//Não era para existir esses JDialogs (cadAvaliacao, cadDisciplina, cadProfessor, cadQuestao
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btArqDisc;
    private javax.swing.JButton btArqProf;
    private javax.swing.JButton btDisciplina;
    private javax.swing.JButton btEnviar;
    private javax.swing.JButton btProfessor;
    private javax.swing.JMenu conexao;
    private javax.swing.JMenu editar;
    private javax.swing.JMenuItem fecharConexao;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JList listUser;
    private javax.swing.JMenuItem novaConexao;
    private javax.swing.JPanel panelMSG;
    private javax.swing.JPanel panelUser;
    private javax.swing.JMenuItem preferencias;
    private javax.swing.JMenuItem sair;
    private javax.swing.JMenu sobre;
    private javax.swing.JTextArea txtArea;
    private javax.swing.JTextArea txtMSG;
    // End of variables declaration//GEN-END:variables

  @Override
  public void addOuvintes() {
    preferencias.addActionListener(suporteEventos);
    novaConexao.addActionListener(suporteEventos);
    fecharConexao.addActionListener(suporteEventos);
    sair.addActionListener(suporteEventos);
    btEnviar.addActionListener(suporteEventos);
    btProfessor.addActionListener(suporteEventos);
    btDisciplina.addActionListener(suporteEventos);
    btArqDisc.addActionListener(suporteEventos);
    btArqProf.addActionListener(suporteEventos);
    sobre.addMenuListener(suporteEventos);
    this.addWindowListener(suporteEventos);
  }
  // kra isso aqui ta me parencendo redundancia!!! são necessários para 

  @Override
  public void addActionListener(ActionListener l) {
    suporteEventos.addActionOuvinte(l);
  }

  @Override
  public void addWindowOuvinte(WindowListener l) {
    suporteEventos.addWindowOuvinte(l);
  }

  public void addMenuOuvinte(MenuListener aThis) {
    suporteEventos.addMenuOuvinte(aThis);
  }

  @Override
  public void removeActionListener(ActionListener l) {
  }

  @Override
  public void modelConectChange(ConectEvent evt) {
    switch (evt.getID()) {
      case ConectEvent.CONECTADO:
        //   infoMSG.append(evt.getMSG()+"\n");
        this.txtArea.append(evt.getMSG() + "\n");
        ;
        break;
      case ConectEvent.ERRO_CONEXAO:
        desabilitarMenu(evt.getSource().toString());
        break;
      case ConectEvent.MSG:
        this.txtArea.append(evt.getMSG() + "\n");;
        break;
    }
  }

  @Override
  public void setEditableDados(Object[] d) {
  }

  @Override
  public void close() {
    System.exit(0);
  }

  public void enableMenu(String menu) {
    if (menu.equals(Constantes.SAIR)) {
      editar.setEnabled(true);
      btDisciplina.setEnabled(true);
    }
  }

  public void desabilitarMenu(String menu) {
    if (menu.equals(Constantes.SAIR)) {
      editar.setEnabled(false);
      btDisciplina.setEnabled(false);
    }
  }

  public String getMSG() {
    String msg = this.txtMSG.getText();
    return msg;
  }
}